package com.dongdongshop.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("login")
public class PersonController {
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("login")
    public String login(String username, String password, Model model){
        /*
         * 使用shiro编写认证操作
         * */
        //1,获取subject
        Subject subject = SecurityUtils.getSubject();
        //2,封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        //3,执行登录方法
        try{
            subject.login(token);
        }catch (UnknownAccountException e){
            model.addAttribute("message","用户名不存在");
            model.addAttribute("message");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("message","密码不正确");
            model.addAttribute("message");
            return "login";
        }
        System.out.println(model.getAttribute("message"));
        return "admin/index";
    }

}
